<template>
  <view class="uni-container">
    <uni-forms ref="form" :model="formData" validateTrigger="bind" label-width="100px">
      <uni-forms-item name="name" label="站点名称" required>
        <uni-easyinput v-model="formData.name"></uni-easyinput>
      </uni-forms-item>
      <uni-forms-item name="location" label="地理位置" required>
         <uni-easyinput v-model="formData.location"></uni-easyinput>
      </uni-forms-item>
      <uni-forms-item name="address" label="详细地址" required>
        <uni-easyinput v-model="formData.address"></uni-easyinput>
      </uni-forms-item>
      <uni-forms-item name="contact" label="联系电话" required>
        <uni-easyinput v-model="formData.contact"></uni-easyinput>
      </uni-forms-item>
      <uni-forms-item name="images" label="站点相册" required>
		    <uni-file-picker file-mediatype="image" return-type="array" v-model="formData.images"></uni-file-picker>
      </uni-forms-item>
      <uni-forms-item name="booking_policy" label="预定说明" required>
        <uni-easyinput v-model="formData.booking_policy"></uni-easyinput>
      </uni-forms-item>
      <uni-forms-item name="cancellation_policy" label="取消政策" required>
        <uni-easyinput v-model="formData.cancellation_policy"></uni-easyinput>
      </uni-forms-item>
      <uni-forms-item name="status" label="状态" required>
        <uni-data-checkbox v-model="formData.status" :localdata="formOptions.status_localdata"></uni-data-checkbox>
      </uni-forms-item>
      <view class="uni-button-group">
        <button type="primary" class="uni-button" style="width: 100px;" @click="submit">提交</button>
        <navigator open-type="navigateBack" style="margin-left: 15px;">
          <button class="uni-button" style="width: 100px;">返回</button>
        </navigator>
      </view>
    </uni-forms>
  </view>
</template>

<script>
  import { validator } from '../../js_sdk/validator/mydb-depot-list.js';

  const db = uniCloud.database();
  const dbCmd = db.command;
  const dbCollectionName = 'mydb-depot-list';

  function getValidator(fields) {
    let result = {}
    for (let key in validator) {
      if (fields.includes(key)) {
        result[key] = validator[key]
      }
    }
    return result
  }

  

  export default {
    data() {
      let formData = {
        "name": "",
        "location": new db.GeoPoint(114.044805, 22.904202),
        "address": "",
        "contact": "",
        "images": [],
        "booking_policy": "",
        "cancellation_policy": "",
        "status": 1,
      }
      return {
        formData,
        formOptions: {
          "status_localdata": [
            {
              "value": 0,
              "text": '启用'
            },
            {
              "value": 1,
              "text": '关闭'
            }
          ]
        },
        rules: {
          ...getValidator(Object.keys(formData))
        }
      }
    },
    onReady() {
      this.$refs.form.setRules(this.rules)
    },
    methods: {
      
      /**
       * 验证表单并提交
       */
      submit() {
        uni.showLoading({
          mask: true
        })
        this.$refs.form.validate().then((res) => {
          return this.submitForm(res)
        }).catch(() => {
        }).finally(() => {
          uni.hideLoading()
        })
      },

      /**
       * 提交表单
       */
      submitForm(value) {
        // 使用 clientDB 提交数据
        return db.collection(dbCollectionName).add(value).then((res) => {
          uni.showToast({
            title: '新增成功'
          })
          this.getOpenerEventChannel().emit('refreshData')
          setTimeout(() => uni.navigateBack(), 500)
        }).catch((err) => {
          uni.showModal({
            content: err.message || '请求服务失败',
            showCancel: false
          })
        })
      }
    }
  }
</script>
